package cn.alm.template_backend_project.controller;

import cn.alm.template_backend_project.common.CommonResponse;
import cn.alm.template_backend_project.entity.LoginForm;
import cn.alm.template_backend_project.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @PostMapping("login")
    public CommonResponse login(@RequestBody LoginForm loginForm) {
        return authService.login(loginForm);
    }

    @PostMapping("logout")
    public CommonResponse logout(@RequestHeader(value = "Authorization", required = false) String authorization) {
        String token = null;
        if (authorization != null && authorization.startsWith("Bearer ")) {
            token = authorization.substring(7);
        }
        return authService.logout(token);
    }
}